package ai.mspbots.poc.listener;

import ai.mspbots.poc.help.PocEnv;
import org.deeplearning4j.models.embeddings.loader.WordVectorSerializer;
import org.deeplearning4j.models.embeddings.wordvectors.WordVectors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

import java.io.File;


@Component
public class ApplicationContextRefreshListener {

    private static final Logger logger = LoggerFactory.getLogger(ApplicationContextRefreshListener.class);

    @Autowired
    private PocEnv pocEnv;


    private final ApplicationContext applicationContext;

    public ApplicationContextRefreshListener(ApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
    }
    @EventListener
    public void handleContextRefresh(ContextRefreshedEvent event) {
        // 在 Spring Context 启动完成后添加一个新的 Bean

        // applicationContext.getAutowireCapableBeanFactory().initializeBean(wordVectors, "wordVectors");
        logger.info("WordVectors loaded..............");
    }

    @Bean
    public WordVectors wordVectors() {
        return WordVectorSerializer.loadStaticModel(new File(pocEnv.getGlove().getModel()));
    }
}
